package com.pdstudio.apapter;

import java.util.List;
import com.pdstudio.bean.WaterStation;
import com.pdstudio.view.ViewHolder;
import com.pdstudio.weather.R;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class WaterStationAdapter extends BaseAdapter {

	private List<WaterStation> mStations;
	private LayoutInflater inflater;
	private Context mContext;

	public WaterStationAdapter(Context context, List<WaterStation> stations) {
		mStations = stations;
		inflater = LayoutInflater.from(context);
		mContext = context;
	}

	@Override
	public int getCount() {
		if (mStations == null) {
			return 0;
		} else {
			return (mStations.size());
		}
	}

	@Override
	public Object getItem(int position) {
		if (mStations == null) {
			return null;
		} else {

			return mStations.get(position);

		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		// 观察convertView随ListView滚动情况
		Log.v("MyListViewBase", "getView " + position + " " + convertView);
		try {
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.item_listview_water,
						null);
				holder = new ViewHolder();
				/** 得到各个控件的对象 */
				holder.name = (TextView) convertView
						.findViewById(R.id.water_station_name);
				holder.trendbtn = (ImageView) convertView
						.findViewById(R.id.water_station_trend);
				holder.position = (TextView) convertView
						.findViewById(R.id.water_station_position);
				holder.flow = (TextView) convertView
						.findViewById(R.id.water_station_flow);
				holder.posttime = (TextView) convertView
						.findViewById(R.id.water_station_posttime);
				holder.warning = (TextView) convertView
						.findViewById(R.id.water_station_jingjie);
				holder.trend = (TextView) convertView
						.findViewById(R.id.water_station_shuishi);
				holder.trendImg = (ImageView) convertView
						.findViewById(R.id.water_station_trendimg);
				holder.linearlayout = (LinearLayout) convertView
						.findViewById(R.id.water_linearlayout);
				convertView.setTag(holder);// 绑定ViewHolder对象
			}

			else {
				holder = (ViewHolder) convertView.getTag();// 取出ViewHolder对象
			}

			/** 设置TextView显示的内容，即我们存放在动态数组中的数据 */

			holder.name.setText("站名："
					+ mStations.get(position).getStationName());
			holder.position.setText("水位："
					+ mStations.get(position).getWaterPosition());
			holder.flow.setText("流量：" + mStations.get(position).getWaterFlow());
			holder.posttime.setText("[" + mStations.get(position).getDate()
					+ "报汛]");
			holder.warning.setText("警戒水位："
					+ mStations.get(position).getWarning());
			holder.trend.setText("水势：");
			holder.trendImg.setTag(mStations.get(position).getId());
			switch (mStations.get(position).getWaterDirection()) {
			case 0:
				holder.trendImg
						.setBackgroundResource(R.drawable.water_trend_non);
				break;
			case 1:
				holder.trendImg
						.setBackgroundResource(R.drawable.water_trend_up);
				break;
			case 2:
				holder.trendImg
						.setBackgroundResource(R.drawable.water_trend_down);
				break;
			}

			switch (mStations.get(position).getWarningFlag()) {
			case 0:
				holder.linearlayout
						.setBackgroundResource(R.color.linearlayout_background);
				break;
			case 1:// 黄色预警
				holder.linearlayout.setBackgroundResource(R.color.water_green);
				break;
			case 2:// 红色预警
				holder.linearlayout.setBackgroundResource(R.color.water_red);
				break;
			}

			/** 为item设置背景颜色 */
			holder.trendbtn.setTag(mStations.get(position).getId());

		} catch (Exception e) {
			e.printStackTrace();
		}

		return convertView;
	}

}
